<script>
export default {
  data() {
    return {
      pro_code: "",
	  scene: "",
      token: "",
      code: "",
      wxUserInfo: "",
      openId: "",
      unionId: "",
    };
  },
  onLaunch: function (e) {
	// scene 需要使用 decodeURIComponent 才能获取到生成二维码时传入的 scene
	this.scene = decodeURIComponent(e.query.scene)
    this.pro_code = e.query.invite_code;
    this.token = uni.getStorageSync("token");
    // 微信登录
    this.login();
  },
  methods: {
    login() {
      let that = this;
      wx.login({
        success(res) {
          that.code = res.code;
          that.getOpenId();
        },
      });
    },
    async getOpenId() {
      let that = this;
      const data = await that.$myRequest({
        url: "index/getOpenId",
        method: "POST",
        data: {
          code: that.code,
        },
      });
      that.openId = data.data.data.openId;
      that.unionId = data.data.data.unionId;
      if (!that.token) {
        wx.showModal({
          title: "温馨提示",
          content: "正在请求您的个人信息",
          success(res) {
            if (res.confirm) {
              wx.getUserProfile({
                desc: "获取你的昵称、头像、地区及性别",
                success: (res) => {
                  that.wxUserInfo = res.userInfo;
                  that.adduser();
                },
                fail: (res) => {
                  //拒绝授权
                  uni.showToast({
                    title: "您拒绝了请求",
                    icon: "none",
                  });
                  return;
                },
              });
            } else if (res.cancel) {
              uni.showToast({
                title: "您拒绝了请求",
                icon: "none",
              });
              return;
            }
          },
        });
      }
    },
    async adduser() {
      let that = this;
      const datas = await that.$myRequest({
        url: "user/add_users",
        method: "POST",
        data: {
          avatarUrl: that.wxUserInfo.avatarUrl,
          nickname: that.wxUserInfo.nickName,
          openId: that.openId,
          gender: that.wxUserInfo.gender,
          province: that.wxUserInfo.province,
          country: that.wxUserInfo.country,
          city: that.wxUserInfo.city,
          unionId: that.unionId,
          fromUserId: that.scene || "",
          fromByXcx: 1,
        },
      });
      if (datas.data.code == 1) {
        uni.setStorageSync("token", datas.data.data.token);
        uni.setStorageSync("user", datas.data.data);
        that.getuserInfo();
      }
    },

    // 获取用户信息
    async getuserInfo() {
      let that = this;
      const res = await that.$myRequest({
        url: "user/getUserInfo",
        method: "GET",
        data: {},
      });
      if (res.data.code == 1) {
        that.userInfo = res.data.data;
        uni.setStorageSync("userInfo", res.data.data);
      }
    },
  },
};
</script>

<style lang="scss">
.navbar {
  display: flex;
  align-items: flex-end;
  background-color: #c52b2e;
  width: 100%;
  height: 90rpx;
  position: fixed;
  z-index: 999 !important;

  // margin-top: 65rpx;
}

.interval {
  padding-top: 108rpx;
}

.search_box {
  display: flex;
  align-items: center;
  // padding-right: 20rpx;
  height: 110rpx;
  line-height: 110rpx;
  width: 100%;
  box-sizing: border-box;
  // padding-top: 10rpx;
  background-color: #c52b2e;
  position: fixed;
  z-index: 999 !important;

  .goback {
    width: 22rpx;
    // margin: 0 0rpx 0 28rpx;
    position: relative;
    left: 28rpx;
  }

  .topicText {
    font-size: 35rpx;
    color: #ffffff;
    margin: 0 auto;
    // padding-left: 70rpx;
    letter-spacing: 2rpx;
    font-weight: 700;
  }

  .search {
    display: flex;
    align-items: center;
    width: 82%;
    background-color: #fff;
    height: 69rpx;
    border-radius: 20px;
    margin: 0 0 0 28rpx;

    .searchImg {
      width: 49rpx;
      height: 43rpx;
      margin-left: 20rpx;
    }

    .searchInp {
      font-size: 30rpx;
      color: #333;
      padding-left: 10rpx;
    }
  }

  .userImg {
    width: 69rpx;
    height: 69rpx;
    margin: 0 20rpx;
  }
}

.worBtn {
  display: flex;
  align-items: center;
  justify-content: center;
  color: #fff;
  font-size: 30rpx;
  letter-spacing: 2rpx;
  width: 211rpx;
  height: 69rpx;
  background-color: #c52b2e;
  border-radius: 35rpx;
  position: fixed;
  bottom: 100rpx;
  left: 50%;
  transform: translate(-50%, -50%);
}
uni-tabbar {
  .uni-tabbar {
    box-shadow: 0px -1px 1px 0px rgba(204, 204, 204, 0.5);
    .uni-tabbar-border {
      background-color: transparent !important; // tabBar上边框的颜色
    }
    .uni-tabbar__icon {
      width: 60rpx;
      height: 40rpx;
    }
    .uni-tabbar__label {
      font-weight: bold;
    }
  }
}

/*每个页面公共css */
</style>
